#include "User.h"
#include <header.h>

User* createUser(int uid, const char* uname, const char* salt, const char* cryptpwd, const char* depository) {
    User* user = (User*)calloc(1, sizeof(User));
    if (user == NULL) {
        perror("Failed to allocate memory for User");
        return NULL;
    }

    user->uid = uid;
    strncpy(user->uname, uname, sizeof(user->uname) - 1);
    user->uname[sizeof(user->uname) - 1] = '\0';

    strncpy(user->salt, salt, sizeof(user->salt) - 1);
    user->salt[sizeof(user->salt) - 1] = '\0';

    strncpy(user->cryptpwd, cryptpwd, sizeof(user->cryptpwd) - 1);
    user->cryptpwd[sizeof(user->cryptpwd) - 1] = '\0';

    snprintf(user->depository, sizeof(user->depository), "%s%s","/", depository);

    return user;
}

void setUid(User* user, int uid) {
    if (user) {
        user->uid = uid;
    }
}

int getUid(const User* user) {
    return user ? user->uid : -1;
}

void setUname(User* user, const char* uname) {
    if (user && uname) {
        strncpy(user->uname, uname, sizeof(user->uname) - 1);
        user->uname[sizeof(user->uname) - 1] = '\0';
    }
}

const char* getUname(const User* user) {
    return user ? user->uname : NULL;
}

void setSalt(User* user, const char* salt) {
    if (user && salt) {
        strncpy(user->salt, salt, sizeof(user->salt) - 1);
        user->salt[sizeof(user->salt) - 1] = '\0';
    }
}

const char* getSalt(const User* user) {
    return user ? user->salt : NULL;
}

void setCryptpwd(User* user, const char* cryptpwd) {
    if (user && cryptpwd) {
        strncpy(user->cryptpwd, cryptpwd, sizeof(user->cryptpwd) - 1);
        user->cryptpwd[sizeof(user->cryptpwd) - 1] = '\0';
    }
}

const char* getCryptpwd(const User* user) {
    return user ? user->cryptpwd : NULL;
}

void setDepository(User* user, const char* depository) {
    if (user && depository) {
        strncpy(user->depository, depository, sizeof(user->depository) - 1);
        user->depository[sizeof(user->depository) - 1] = '\0';
    }
}

const char* getDepository(const User* user) {
    return user ? user->depository : NULL;
}

void destroyUser(User* user) {
    if (user) {
        free(user);
    }
}
